package me.project.jobster.service;

import me.project.jobster.common.constant.GlobalConstants;
import me.project.jobster.common.util.DateUtils;
import me.project.jobster.common.util.GUIDGenerator;
import me.project.jobster.dao.mybatis.mapper.PositionMapper;
import me.project.jobster.dao.mybatis.model.Position;
import me.project.jobster.dao.mybatis.model.Student;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Mcdull
 * @date 2018-4-27
 */
@Service
public class JobService {

    @Autowired
    private PositionMapper positionMapper;
    @Autowired
    private JdbcTemplate jdbcTemplate;


    /**
     * get all position without paging
     *
     * @return
     */
    public List<Position> getJobList() {
        return positionMapper.selectAll();
    }

    /**
     * get position without paging
     *
     * @return
     */
    public List<Position> getJobListConditional(String userId) {
        Position position = new Position();
        position.setcId(userId);
        return positionMapper.select(position);
    }

    /**
     * get the position information
     *
     * @return
     */
    public Position getJobInfo(String positionId) {
        Position position = new Position();
        position.setpId(positionId);
        return positionMapper.selectOne(position);
    }

    /**
     * fuzzy search
     * @param position
     * @return
     */
    public List<Position> getJobListFuzzy(Position position) {
        StringBuilder sql = new StringBuilder("select * from position where 1=1 ");
        List<Object> queryList = new ArrayList<Object>();
        if (!StringUtils.isEmpty(position.getpId())) {
            sql.append(" and p_id like concat('%',?,'%')");
            queryList.add(position.getpId().trim());
        }
        if (!StringUtils.isEmpty(position.getTitle())) {
            sql.append(" and title like concat('%',?,'%')");
            queryList.add(position.getTitle().trim());
        }
        if (!StringUtils.isEmpty(position.getSalary())) {
            sql.append(" and salary like concat('%',?,'%')");
            queryList.add(position.getSalary().trim());
        }
        if (!StringUtils.isEmpty(position.getLocation())) {
            sql.append(" and location like concat('%',?,'%')");
            queryList.add(position.getLocation().trim());
        }
        if (!StringUtils.isEmpty(position.getRemajor())) {
            sql.append(" and remajor like concat('%',?,'%')");
            queryList.add(position.getRemajor().trim());
        }
        if (!StringUtils.isEmpty(position.getRedegree())) {
            sql.append(" and redegree like concat('%',?,'%')");
            queryList.add(position.getRedegree().trim());
        }
        return jdbcTemplate.query(sql.toString(), queryList.toArray(), new BeanPropertyRowMapper<>(Position.class));

    }


    /**
     * save a job
     *
     * @param position
     */
    public void postJob(Position position) {
        position.setpId(GUIDGenerator.getInstance().generateUID());
        position.setTime(DateUtils.formatNow("yyyy-MM-dd HH:mm:ss:SSS"));
        positionMapper.insert(position);
    }
}
